package cn.iocoder.yudao.module.system.controller.admin.mini.goodInfo.amway;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.system.controller.admin.mini.goodInfo.amway.mapper.AmwayMapper;
import cn.iocoder.yudao.module.system.controller.admin.mini.goodInfo.amway.vo.AmwayAddVo;
import cn.iocoder.yudao.module.system.controller.admin.mini.goodInfo.amway.vo.AmwayDO;
import cn.iocoder.yudao.module.system.controller.admin.mini.goodInfo.amway.vo.AmwayInfoVo;
import cn.iocoder.yudao.module.system.controller.admin.mini.goodInfo.amway.vo.AmwayVo;
import cn.iocoder.yudao.module.system.controller.admin.mini.goodInfo.goods.dal.dataobject.goods.GoodsDO;
import cn.iocoder.yudao.module.system.controller.admin.mini.goodInfo.goods.dal.mysql.goods.GoodsMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

@Tag(name = "商品推荐")
@RestController
@RequestMapping("/qd/amway")
public class AmwayController {

    @Resource
    AmwayMapper amwayMapper;
    @Resource
    GoodsMapper goodsMapper;
    @Resource
    RedisTemplate<String, Object> redis;


    @GetMapping("getTime")
    public CommonResult getTime(){
        LocalDateTime time = LocalDateTime.now();
        Object o = redis.opsForValue().get("admin_amwayTime");
        if (o!=null){
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");
            time = LocalDateTime.parse(o.toString(), formatter);
        }

        return CommonResult.success(time);
    }

    @GetMapping("setTime")
    public CommonResult setTime(Long time){
        Instant instant = Instant.ofEpochMilli(time);
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault()); // 根据系统默认时区将Instant转换为LocalDateTime

        redis.opsForValue().set("admin_amwayTime",localDateTime.toString());
        return CommonResult.success("");
    }


    //后台-好物精选页面
    @GetMapping("page")
    public CommonResult page(){
        return CommonResult.success(amwayMapper.selectList(new LambdaQueryWrapper<>()));
    }

    //通过id获取数据
    @GetMapping("getById")
    public CommonResult getById(Integer id){
        return CommonResult.success(amwayMapper.selectById(id));
    }

    //后台-好物精选-添加商品
    @PostMapping("addList")
    public CommonResult addList(@RequestBody AmwayAddVo vo){
        AmwayDO amwayDO = amwayMapper.selectById(vo.getId());
        Integer max = getMax(amwayDO.getInfos());
        List<AmwayVo> infos = amwayDO.getInfos();
        for (AmwayVo info : vo.getList()) {
//            info.setUuid(UUID.randomUUID().toString());
            info.setOrder(max);
            infos.add(info);
        }
        amwayDO.setInfos(infos);

        amwayMapper.updateById(amwayDO);
        return CommonResult.success("");
    }

    //后台-好物精选-商品删除
    @DeleteMapping("deleteById")
    public CommonResult deleteById(Integer id,Integer goodId){
        AmwayDO amwayDO = amwayMapper.selectById(id);
        Iterator<AmwayVo> iterator = amwayDO.getInfos().iterator();
        while (iterator.hasNext()){
            AmwayVo next = iterator.next();
            if (next.getGoodId().equals(goodId)){
                iterator.remove();
                break;
            }
        }
        amwayMapper.updateById(amwayDO);
        return CommonResult.success("");
    }

    //修改
    @PutMapping("update")
    public CommonResult update(@RequestBody AmwayDO dto){
        amwayMapper.updateById(dto);
        return CommonResult.success("");
    }

    //分类-商品分页
    @GetMapping("amwayPage")
    public CommonResult AmwayPage(Integer pageNo,Integer pageSize,Integer id){
        AmwayDO amwayDO = amwayMapper.selectById(id);
        List<AmwayVo> infos = amwayDO.getInfos();
        long size = infos.size();
        infos.sort(Comparator.comparingInt(AmwayVo::getOrder));

        List<AmwayVo> goods = infos.stream().skip((long) (pageNo - 1) * pageSize).limit(pageSize).collect(Collectors.toList());
        List<Integer> goodIds = goods.stream().map(AmwayVo::getGoodId).collect(Collectors.toList());

        PageResult<GoodsDO> result = new PageResult<>();
        result.setList(goodsMapper.selectList(new LambdaQueryWrapper<GoodsDO>().in(GoodsDO::getId, goodIds)));
        result.setTotal(size);

        return CommonResult.success(result);
    }

    //小程序首页-商品推荐
    @GetMapping("amwayInfos")
    public CommonResult AmwayInfos(){
        List<AmwayInfoVo> list=new ArrayList<>();

        List<AmwayDO> amwayDOS = amwayMapper.selectList(new LambdaQueryWrapper<>());
        for (AmwayDO amwayDO : amwayDOS) {
            List<GoodsDO> goodsDOS=new ArrayList<>();
            List<AmwayVo> infos = amwayDO.getInfos();
            for(int i=0;i<infos.size();i++){
                if (i>5)
                    break;
                goodsDOS.add(goodsMapper.selectById(infos.get(0).getGoodId()));
            }
            list.add(new AmwayInfoVo(amwayDO.getName(),amwayDO.getUrl(),goodsDOS));
        }

        return CommonResult.success(list);
    }


    public Integer getMax(List<AmwayVo> vos){
        Integer max=0;
        for (AmwayVo vo : vos) {
            if (vo.getOrder()>max)
                max=vo.getOrder();
        }
        return max;
    }

    public static void main(String[] args) {
        System.out.println(UUID.randomUUID().toString());
    }
}
